//
//  MPClueViewController.m
//  SearchPeople
//
//  Created by zk on 16/5/6.
//
//

#import "MPClueViewController.h"
#import "MPClueTableViewCell.h"
#import "MPClueCommentTableViewCell.h"
#import "MPClueImageTableViewCell.h"
#import "MPCommentViewController.h"
#import <UMSocialSnsService.h>
#import <UMSocialConfig.h>
#import "UMSocial.h"
#import "MPDocumentHttpClient.h"
#import "MPSettingsManager.h"
#import <SVProgressHUD.h>
#import <UIImageView+WebCache.h>


@interface MPClueViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
UMSocialUIDelegate
>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic) NSMutableArray *commentArray;

@property (strong, nonatomic) UIImageView *shareImageView;

@end

@implementation MPClueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self TableviewAttribute];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self Comment];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private

- (void)Comment{
    [[MPDocumentHttpClient shareInstance] GetCommentWithMPID:self.dic[@"id"] WithSuccessBlock:^(MPRequest *request, MPResponse *response) {
        
        NSDictionary *dic = response.result;
        self.commentArray = dic[@"data"];
        [self.tableview reloadData];
    } WithFailedBlock:^(MPRequest *request, MPResponse *response) {
        
    }];
}

- (void)TableviewAttribute
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.tableFooterView = [UIView new];
}

#pragma mark - UITableView delegate & datasource 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    } else {
        return self.commentArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{
                MPClueTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPClueTableViewCell"];
                [cell configWithDic:self.dic];
                return cell;
            }
                break;
            case 1:{
                MPClueImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPClueImageTableViewCell"];
                [cell configWithDic:self.dic];
                return cell;
            }
                break;
            default:{
                MPClueCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPClueCommentTableViewCell"];
                return cell;
            }
                break;
        }

    } else {
        MPClueCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPClueCommentTableViewCell"];
        [cell configWithDic:self.commentArray[indexPath.row]];
        return cell;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return @"评论";
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                return 60;
                break;
            case 1:
                return self.view.frame.size.width + 100;
                break;
            default:
                return 50;
                break;
        }
    } else {
        return 44;
    }
}

#pragma mark - 分享

- (IBAction)ButtonClick:(id)sender {
    UIButton *button = (UIButton *)sender;
    switch (button.tag) {
        case 1:
        {
            MPCommentViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MPCommentViewController"];
            targetVC.MPID = self.dic[@"id"];
            [self.navigationController pushViewController:targetVC animated:YES];
        }
            break;
        case 2:
        {
            MPClueImageTableViewCell *cell = [self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
            NSString *lostName = self.dic[@"name"];
            NSString *lostAddress = self.dic[@"address"];
            NSString *lostTime = self.dic[@"time"];
            NSString *shareText = [NSString stringWithFormat:@"失踪者：%@ 失踪地点：%@ 失踪时间：%@",lostName,lostAddress,lostTime];             //分享内嵌文字
            //分享内嵌图片
                       //调用快速分享接口
            [UMSocialData defaultData].extConfig.wechatSessionData.title = @"missing person";
            [UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
            [UMSocialData defaultData].extConfig.wechatSessionData.url = @"http://baidu.com";
            [UMSocialSnsService presentSnsIconSheetView:self
                                                 appKey:@"572af90367e58e33f0000715"
                                              shareText:shareText
                                             shareImage:cell.ImageView.image
                                        shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatTimeline,UMShareToWechatSession,nil]
                                               delegate:self];
            
        }
            break;
        case 3:
        {
            NSDictionary *userDic = [[MPSettingsManager getInstance].userDefault objectForKey:@"user"];
            NSString *user_id = [userDic objectForKey:@"user_id"];
            [[MPDocumentHttpClient shareInstance] AttentionWithMPID:self.dic[@"id"] WithUserID:user_id WithSuccessBlock:^(MPRequest *request, MPResponse *response) {
                [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
            } WithFailedBlock:^(MPRequest *request, MPResponse *response) {
                
            }];
            
        }
            break;
        default:
            break;
    }
}
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}

#pragma mark - Setter && Getter

- (NSMutableArray *)commentArray{
    if (!_commentArray) {
        _commentArray = [[NSMutableArray alloc] init];
    }
    return _commentArray;
}

@end
